import { createStore } from "vuex"
import {storage} from "../../packages"
// import router from "@/router/index.js"
//
export default createStore({
  state:() => ({
    token:'',
    user:undefined,
    menus:undefined,
    permissions:undefined,
    routes:undefined
  }),
  getters:{
    token:(state) => state.token || storage.get('token'),
    user:(state) => state.user || storage.get('user','object') || {},
    menus:(state) => state.menus || storage.get('menus','object') || [],
    permissions:(state) => state.permissions || storage.get('permissions','object') || [],
    routes:(state) => state.routes || storage.get('routes','object') || [],
  },
  mutations:{
    setToken(state,val){state.token = val;storage.add('token',val)},
    setUser(state,val){state.user = val;storage.add('user',val)},
    setMenus(state,val){state.menus = val;storage.add('menus',val)},
    setPermissions(state,val){state.permissions = val;storage.add('permissions',val)},
    setRoutes(state,val){state.routes = val;storage.add('routes',val)},
  },
  actions:{
    async clearUserInfo({state},isClear = true){
      state.token = '';
      state.user = undefined;
      state.menus = undefined;
      state.permissions = undefined;
      state.routes = undefined;
      //
      isClear && storage.clear();
    },
    async logout({dispatch}){
      // 清空 用户数据
      await dispatch('clearUserInfo');
      // 路由到登陆页
      // router.$goLogin();

    }
  }
})